package pl.edu.agh.inpema.components.receivers.phpBB2Receiver.connector;

import java.util.LinkedList;
import java.util.List;

import org.htmlparser.lexer.Page;
import org.htmlparser.util.ParserException;

import pl.edu.agh.inpema.components.receivers.phpBB2Receiver.model.HtmlPost;
import pl.edu.agh.inpema.components.receivers.phpBB2Receiver.model.Post;
import pl.edu.agh.inpema.components.receivers.phpBB2Receiver.model.Subject;
import pl.edu.agh.inpema.components.receivers.phpBB2Receiver.model.Topic;
import pl.edu.agh.inpema.components.receivers.phpBB2Receiver.parser.ForumParser;

public class ForumPageReceiver {
	
	private ForumConnector client;
	private ForumParser parser;
	
	public ForumPageReceiver(String url, String login, String password, String postDetailsPattern, boolean asHtml, long sleepTime) throws ConnectionException {
		this.client = new ForumConnector(url, sleepTime);
		this.parser = new ForumParser(postDetailsPattern, asHtml);
		if (login != null) {
			client.performLogin(login, password);
		}
		
		String mainPage = this.client.getMainPage();
		String charset = Page.DEFAULT_CHARSET;
		try {
			String contentType = parser.getContentType(mainPage);
			Page page = new Page();
			charset = page.getCharset(contentType);
		} catch (ParserException e) {
			e.printStackTrace();
		}
		
		//System.out.println("using charset: "+charset);
		this.client.setCharset(charset);		
	}

	public List<Subject> getAllSubjects() throws ConnectionException, ParserException {
		return this.parser.parseMainPage(this.client.getMainPage());
	}
	
	public List<Subject> getAllSubjects(String pageAddress) throws ConnectionException, ParserException {
		return this.parser.parseMainPage(this.client.getPage(pageAddress));
	}
	
	public List<Topic> getAllTopicsFromSubjectAddress(String address) throws ConnectionException, ParserException {
		List<Topic> allTopics = new LinkedList<Topic>();
		int topicsNumber = 0;		
		List<Topic> topics = parser.parseTopicsPage(client.getPage(address+"&topicdays=0&start="+topicsNumber));
		allTopics.addAll(topics);
		boolean somethingNew = true;
		while (topics.size() > 0 && somethingNew == true) {
			//System.out.println("start loop - "+topics.size() + " - " + somethingNew);
			topicsNumber += topics.size();
			somethingNew = false;
			topics = parser.parseTopicsPage(client.getPage(address+"&topicdays=0&start="+topicsNumber));
			for (Topic topic : topics) {
				if (! allTopics.contains(topic)) {
					allTopics.add(topic);
					somethingNew = true;
				}
			}
			//System.out.println("getAllTopicsFromSubjectAddress - "+topicsNumber + " - " + somethingNew);
		}
		return allTopics;
	}
	
	public List<Post> getAllPostsFromTopicAddress(String address) throws ConnectionException, ParserException {
		List<Post> allPosts = new LinkedList<Post>();
		int postsNumber = 0;
		List<Post> posts = parser.parsePostsPage(client.getPage(address+"&postdays=0&postorder=asc&start="+postsNumber));
		allPosts.addAll(posts);
		while (posts.size() > 0) {
			postsNumber += posts.size();
			posts = parser.parsePostsPage(client.getPage(address+"&postdays=0&postorder=asc&start="+postsNumber));
			allPosts.addAll(posts);
		}
		return allPosts;
	}
}
